package com.andromouse;

import java.awt.TrayIcon;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingWorker;

public class WifiHandler extends SwingWorker<String, Object> {
	JLabel addressLab;
	JLabel statusLab;
	JButton cancelBut;
	static final int BUFSIZE = 1024;
	public DatagramSocket socket;
	private static TrayIcon trayIcon;
	UDPServer myUDP;
	String clientSentence = null;
	boolean CONNECTED = true;
	ServerSocket TCPServer;
	FileBrowserServer fileServer;
	private int port;

	public WifiHandler(JLabel address, JButton cancel, TrayIcon tray,
			JLabel status) {
		this.addressLab = address;
		this.cancelBut = cancel;
		trayIcon = tray;
		this.statusLab = status;
		Utils.status = 0;

		this.port = Utils.port;
	}

	public String doInBackground() {
		this.addressLab.setText("Working");
		String OSName = System.getProperty("os.name");
		if (OSName.contains("Linux")) {
			try {
				InetAddress addr = InetAddress.getLocalHost();
				Enumeration interfaces = NetworkInterface
						.getNetworkInterfaces();
				while (interfaces.hasMoreElements()) {
					NetworkInterface n = (NetworkInterface) interfaces
							.nextElement();
					if (!n.isLoopback()) {
						Enumeration addresses = n.getInetAddresses();
						while (addresses.hasMoreElements()) {
							InetAddress addr1 = (InetAddress) addresses
									.nextElement();
							if (!addr1.isLinkLocalAddress()) {
								addr = addr1;
							}
						}
					}
				}
				this.addressLab.setText(addr.getHostAddress());
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (SocketException e) {
				e.printStackTrace();
			}
		} else if (OSName.contains("Windows"))
			try {
				InetAddress myaddr = InetAddress.getLocalHost();
				String ipAddr = myaddr.getHostAddress();
				this.addressLab.setText(ipAddr);
				if (ipAddr.toString().equals("127.0.0.1"))
					/*break label280;*/ this.addressLab.setText("No Connection");
			} catch (UnknownHostException localUnknownHostException1) {
			}
		else if (OSName.contains("Mac"))
			try {
				InetAddress myaddr = InetAddress.getLocalHost();
				String ipAddr = myaddr.getHostAddress();
				this.addressLab.setText(ipAddr);
				if (ipAddr.toString().equals("127.0.0.1"))
					this.addressLab.setText("No Connection");
			} catch (UnknownHostException localUnknownHostException2) {
			}
		try {
			label280: this.socket = new DatagramSocket(this.port);
		} catch (IOException e) {
			e.printStackTrace();
		}

		Thread tcpThread = new Thread(new TCPServer());
		tcpThread.start();

		Thread udpThread = new Thread(new UDPServer(this.socket));
		udpThread.start();

		this.fileServer = new FileBrowserServer(8889);
		while (true) {
			switch (Utils.status) {
			case 0:
				if (Utils.btStat)
					break;
				this.statusLab.setText("Disconnected");
				break;
			case 1:
				this.statusLab.setText("Connected");
			}

			if (!tcpThread.isAlive()) {
				tcpThread = new Thread(new TCPServer());
				tcpThread.start();
			}
			if (!udpThread.isAlive()) {
				udpThread = new Thread(new UDPServer(this.socket));
				udpThread.start();
			}
		}
	}

	public void done() {
		System.out.println("User pressed cancel");
		this.addressLab.setText("Cancelled..");
	}

	public void autoIpDetectionTest() {
		DatagramSocket serverSocket = null;
		try {
			serverSocket = new DatagramSocket(this.port);

			byte[] receiveData = new byte[1024];
			byte[] sendData = new byte[1024];

			DatagramPacket receivePacket = new DatagramPacket(receiveData,
					receiveData.length);
			serverSocket.receive(receivePacket);
			String sentence = new String(receivePacket.getData());
			System.out.println("RECEIVED: " + sentence);
			InetAddress IPAddress = receivePacket.getAddress();
			trayMessage("Client detected at "
					+ IPAddress.getCanonicalHostName());
			int port = receivePacket.getPort();
			String capitalizedSentence = InetAddress.getLocalHost()
					.getHostName();
			sendData = capitalizedSentence.getBytes();
			DatagramPacket sendPacket = new DatagramPacket(sendData,
					sendData.length, IPAddress, port);
			serverSocket.send(sendPacket);
			serverSocket.close();
		} catch (IOException e) {
			System.out.println(e.toString() + "ddd");
			serverSocket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void trayMessage(String message) {
		trayIcon.displayMessage("AndroMouse Server", message,
				TrayIcon.MessageType.INFO);
	}
}